package top.codecrab.javase;

import top.codecrab.time.Times;

/**
 * @author codecrab
 * @since 2021年06月26日 11:06
 */
public class Step {

    public static void main(String[] args) {
        int n = 45;
        Integer time1 = Times.test("递归用时测试", () -> f(n));
        System.out.println(time1);
        Integer time2 = Times.test("循环用时测试", () -> fun(n));
        System.out.println(time2);
    }

    public static int fun(int n) {
        if (n < 1) {
            throw new IllegalArgumentException(n + " must be gt 0");
        }
        if (n == 1 || n == 2) {
            return n;
        }

        int one = 1;
        int two = 2;
        int three = 0;
        for (int i = 3; i <= n; i++) {
            three = one + two;
            one = two;
            two = three;
        }
        return three;
    }

    /**
     * 递归法
     * n层楼梯，有多少种走法（斐波那契）
     */
    public static int f(int n) {
        if (n < 1) {
            throw new IllegalArgumentException(n + " must be gt 0");
        }
        if (n == 1 || n == 2) {
            return n;
        }
        return f(n - 2) + f(n - 1);
    }
}
